import urllib
import urllib.request
import re
import time

def getHtml(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)
    return response.read()

def catchImage(html):
    imgRe = re.compile(r'http://[\S]*\.jpg')
    imgList = re.findall(imgRe, repr(html))

    try:
        print("下载开始")
        imgName = 1
        for imgPath in imgList:
            img = getHtml(imgPath)
            with open("pic/" + str(imgName) + ".jpg", 'wb') as fs:
                fs.write(img)
            print("正在下载第%s张图片"%imgName)
            if imgName >= 12:
                break            
            time.sleep(2)            
            imgName += 1
        print("下载完成")
        return
    except Exception as ex:
        print(ex)

html = getHtml("http://tu.duowan.com/m/meinv")
catchImage(html)